﻿using CDEPMS_API.Entities;
using CDEPMS_API.Filters;
using CDEPMS_API.Utils;
using CommHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.AccessControl;
using System.Web.Http;
using System.Web.Http.Results;

namespace CDEPMS_API.Controllers
{
  public class CommonDate
  {
    public string Name { get; set; }

    public string Parameters { get; set; }
  }

  [ApiAuthorize]
  public class DataManagerController : BaseApiController
  {

    [HttpPost]
    public IHttpActionResult Proc(CommonDate CD)
    {

      DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), User.Identity.Name));

      //dm.Procedure("ProcLog", "[{\"Name\":\"" + CD.Name + "\",\"Parm\":\"" + CD.Parameters + "\"}]");

      DataToInfo dataInfo;
      try
      {
        //try
        //{
        //    TrackHandle.SaveTrack(this.Url.Request.RequestUri.ToString(), CD.Name, CD.Parameters);
        //}
        //catch (Exception ex)
        //{
        //ErrorHandle.SaveError(this.Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
        //}

        //TrackHandle.SaveTrackForce("proc", CD.Name, CD.Parameters);
        dataInfo = dm.Procedure(CD.Name, CD.Parameters);
      }
      catch (Exception ex)
      {
        //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
        ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.ToString());
        throw new Exception(ex.ToString());
      }

      return ReturnSuccess(dataInfo.table);
    }


    [HttpPost]
    public IHttpActionResult ProcAddSet(CommonDate CD)
    {

      DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), User.Identity.Name));
      //dm.Procedure("ProcLog", "[{\"Name\":\"" + CD.Name + "\",\"Parm\":\"" + CD.Parameters + "\"}]");


      List<DataToList> res_list = new List<DataToList>();
      try
      {
        List<Dictionary<string, string>> JSONList = JSONManager.ListJsonParse(CD.Parameters);

        for (int i = 0; i < JSONList.Count; i++)
        {
          var jsonParams = "[" + Newtonsoft.Json.JsonConvert.SerializeObject(JSONList[i]) + "]";
          res_list.Add(dm.ProcedureSet(CD.Name, jsonParams));
        }
      }
      catch (Exception ex)
      {
        //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
        ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
        throw new Exception(ex.ToString());
      }

      return ReturnSuccess(res_list);
    }


    [HttpPost]
    public IHttpActionResult ProcSet(CommonDate CD)
    {
      DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), User.Identity.Name));
      DataToList dataList;
      try
      {
        dataList = dm.ProcedureSet(CD.Name, CD.Parameters);
        for (int i = 0; i < dataList.tables.Count; i++)
        {
          if (dataList.tables[i].table.Columns.Contains("PassWord"))
          {
            dataList.tables[i].table.Columns.Remove("PassWord");
          }
        }
      }
      catch (Exception ex)
      {
        //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
        ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
        throw new Exception(ex.ToString());
      }

      return ReturnSuccess(dataList);
    }
  }
}
